#######################################
# Göra om datatyper mellan Perl och R #
#######################################

#!/usr/bin/perl -w
use strict;

use Statistics::R;

my $R = Statistics::R->new() ;

my @vect1 = (1,3,5,6);  ## Skapa en vektor
my $vect2 = join (",",@vect1);  ## Inför kommatecken mellan elementen -> gör om till en sträng

my @vect3 = (1,4,5,7);
my $vect4 = join (",",@vect3);

print "@vect1\n";  ## Hur perl representerar en vektor
print "$vect2\n";  ## Hur R vill att en vektor ska se ut

my @values = (5,10,15,20);  ## Skapa en vektor
my $values2 = join(",",@values);  ## Inför kommatecken 

my @values3 = (3,10,15,18);
my $values4 = join(",",@values3);

my @values5 = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
my $values6 = join(",",@values5);
my @values7 = (1,2,4,6,7,8,9,12,13,14,17,18,20,21,25,49,123,652);
my $values8 = join(",",@values7);

$R->startR();

## Deklarera vektorerna i R

$R->send(qq (v1 <- c($vect2)));  ## Motsvarar: v <- c(1,3,5,6) i R
$R->send(qq (v2 <- c($values2)));  

$R->send(qq (v3 <- c($vect4))); 
$R->send(qq (v4 <- c($values4))); 

$R->send(qq (vect1 <- c($values6))); 
$R->send(qq (vect2 <- c($values8))); 

## Gör saker med vektorerna
my $res;

$R->send(qq (vector1 <- as.data.frame(vect1)));
$R->send(qq (vector2 <- as.data.frame(vect2)));

$R->send(qq (isectval <- intersect(v2, v4)));
$R->send(qq (isectvect <- intersect(v1, v3)));
$R->send(qq (common_order <- intersect(colnames(vector1), colnames(vector2)) \n print(common_order)));

$R->send(qq (d1 <- vector1[,common_order ] \n print(d1)));
$res = $R->read();
print "$res\n";

$R->send(qq (d2 <- vector2[,common_order ] \n print(d2)));
$res = $R->read();
print "$res\n";

$R->send(qq (print(attributes(d1))));
$res = $R->read();
print "$res\n";

$R->send(qq (c(pdf("/home/niklas/Documents/KI/asd.pdf"), plot(isectvect,isectval), dev.off)));

$R->stopR();
